<?php
namespace app\admin\controller;
use think\View;

/**
 * User: wuwenhan
 * Date: 2016/4/12
 * Time: 11:27
 */

class System extends Base
{
    public function index()
    {
        $active['system']['upload'] = 1;
        $this->view->active = $active;

        return $this->view->fetch();
    }
    public function safe()
    {
        $active['safe'] = 1;
        $this->view->active = $active;

        return $this->view->fetch();
    }

    /**
     * 清除缓存
     * @return
     */
    public function clear_cache()
    {
        if(\think\Cache::clear())
            return $this->success('清除缓存成功'); 
        else
            return $this->error('清除缓存失败');
    }

    /**
     * 生成网站地图
     * 马宇翔
     */
    public function createSitemap()
    {
        $list = M('Term')->where(['display_nav'=>1])->field('id,flag,name,create_time')->order('id desc')->select();
        $sitemap = "<?xml version=\"1.0\" encoding=\"UTF-8\"?>\r\n<urlset xmlns=\"http://www.sitemaps.org/schemas/sitemap/0.9\">\r\n";
        foreach($list as $k=>$v){
            $sitemap .= $this->addSitemap($v);
        }
        $sitemap .= '</urlset>';
        $file = fopen(ROOT_PATH."sitemap.xml","w");
        fwrite($file,$sitemap);
        fclose($file);
        return $this->success('地图生成成功');        
    }

    private function addSitemap($arr)
    {
        $sitemap  = "<url>\r\n";
        $sitemap .= "<loc>".U('/t/'.$arr['flag'])."</loc>\r\n";
        $sitemap .= "<priority>".$arr['name']."</priority>\r\n";
        $sitemap .= "<lastmod>".date('Y-m-d',$arr['create_time'])."</lastmod>\r\n";
        $sitemap .= "<changefreq>weekly</changefreq>\r\n";
        $sitemap .= "</url>\r\n";
        return $sitemap;
    }
}
